#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <chrono>
#include <unistd.h>


#include "AudioDecode.h"
#include "AudioDecodeRaw.h"

#define READ_SIZE 4096

AudioDecode* dec;
FILE *fout;

void fun1()  //初始化构造函数
{
    std::cout << "Thread " << 1 << " executing" << std::endl;
    while(1)
    {
        char* buf;
        int ret = dec->decode((uint8_t**)&buf);
        if(ret<0){
            printf("read error, ret = %d\n", ret);
            break;
        }else if(0==ret){
            //std::this_thread::sleep_for(std::chrono::milliseconds(10));
            //
            printf("decode zero, ret = %d\n", ret);
        }else{
            printf("read ret = %d\n", ret);
            fwrite(buf, 1, ret, fout);
        }

    }
    if(fout)
    {
        fclose(fout);
        fout = NULL;
    }
    std::cout << "Thread " << 1 << " exit\n";
}


int main(int argc, char* argv[])
{
    //if(argc<2)
    //{
        //printf("usage:dec_mp3 infile\n");
        //return -1;
    //}

    //输入解码类型
    char format[256] = "mp3";

    //输入进行解码的mp3文件
    char input_file[256] = "test.mp3";

    //输出解码后获取到的pcm音频数据文件
    char output_file[256] = "outdec1.pcm";

    if(argc>1)
    {
        strcpy(input_file, argv[1]);
    }

    for(int i=1;i<argc;i++)
    {
        if (!strcmp(argv[i],"-i"))
        {
            strcpy(input_file, argv[++i]);
        }
        if (!strcmp(argv[i],"-o"))
        {
            strcpy(output_file, argv[++i]);
        }
        if (!strcmp(argv[i],"-f"))
        {
            strcpy(format, argv[++i]);
        }
        //if (!strcmp(argv[i],"-t"))
        //{
            //test_times = atoi(argv[++i]);
        //}
        if (!strcmp(argv[i],"-h"))
        {
            printf("usage: dec_test -f format -i input_file -o output_file \n");
            return 0;
        }
    }


start:
    //dec.init();
    //dec.init(AV_CODEC_ID_MP3);

    dec = new AudioDecode();

    FILE *fin;

    fin = fopen(input_file, "rb");
    if (!fin) {
        fprintf(stderr, "Could not open %s\n", input_file);
        exit(1);
    }
    printf("open input_file %s ok\n", input_file);
    fout = fopen(output_file, "wb");
    if (!fout) {
        exit(1);
    }
    printf("open output_file %s ok\n", output_file);

    dec->start();

    std::thread t1(fun1);

    while(!feof(fin))
    {
        uint8_t in_buffer[READ_SIZE];
        int in_len = fread(in_buffer, 1, READ_SIZE, fin);
        int sz = 0;
        while(sz<in_len)
        {
            int out_len = dec->send_data((const uint8_t*)in_buffer+sz, in_len-sz);
            //printf("send size = %d\n", out_len);
            if (out_len < 0) {
                fprintf(stderr, "Error while send_data\n");
                exit(1);
            }
            //printf("out size = %d\n", ret);
            sz += out_len;
        }
    }

    //usleep(10*1000*1000);

    dec->stop();
    printf("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n");
    t1.join();
    fclose(fin);
    if(fout)
    {
        fclose(fout);
        fout = NULL;
    }

    dec->destroy();
    printf("AOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO\n");
    delete dec;

    //goto start;

    //usleep(999999999999999);
    return 0;
}


